<template>
  <div>
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag"></div>
    </transition>

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">{{goodsinfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>
            <span>市场价:<del>{{goodsinfo.market_price}}</del></span>&nbsp;
            <span style="color:red;font-weight:bold;">销售价:{{goodsinfo.sell_price}}</span>
          </p>
          <p class="numbox">
            购买数量：&nbsp;<numbox @getcunt="getSelectedCount" :max="goodsinfo.stock_quantity"></numbox>
          </p>
          <br>
          <div>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="appendCar">加入购物车</mt-button>
          </div>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号:{{goodsinfo.goods_no}}</p>
          <p>库存情况:{{goodsinfo.stock_quantity}}</p>
          <p>上架时间:{{goodsinfo.add_time}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" @click="goodsPresent(goodsinfo.id)">商品介绍</mt-button>
        <br>
        <mt-button type="danger" size="large" plain @click="goodsComment(goodsinfo.id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swiper from "../subcponents/swiper.vue";
import numbox from "../subcponents/numbox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbotu: [],
      goodsinfo: {},
      ballFlag: false,
      selectedCount: 1
    };
  },
  created() {
    this.getLunBoTu();
    this.getGoodsInfo();
  },
  methods: {
   async getLunBoTu() {
     await this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.data.status == 0) {
          result.data.message.forEach(item => {
            item.img = item.src;
          });
          this.lunbotu = result.data.message;
        }
      });
    },
   async getGoodsInfo() {
    await  this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.data.status == 0) {
          this.goodsinfo = result.data.message[0];
        }
      });
    },
    goodsPresent(id) {
      this.$router.push({ name: "goodspresent", params: { id } });
    },
    goodsComment(id) {
      this.$router.push({ name: "goodscomment", params: { id } });
    },
    appendCar() {
      //购物车添加
      //id 价格 数量 是否购买选中四个参数
      this.ballFlag = !this.ballFlag;
      var goodsdata = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsinfo.sell_price,
        selected: true
      };
      //调用仓库方法
      this.$store.commit("addToCar",goodsdata);
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.style.offsetWidth;
      const ballPosition = document
        .querySelector(".ball")
        .getBoundingClientRect();
      const shopCarPosition = document
        .querySelector("#badge")
        .getBoundingClientRect();
      // console.log(ballPosition, shopCarPosition);
      const DiscX = shopCarPosition.left - ballPosition.left;
      const DiscY = shopCarPosition.top - ballPosition.top;
      el.style.transform = `translate(${DiscX}px,${DiscY}px)`;
      el.style.transition = "all 0.5s ease";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count) {
      this.selectedCount = count;
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>

<style scoped>
.mui-card-header {
  font-size: 14px;
  border-radius: 2px 2px 0 0;
  font-weight: bold;
}
.mui-card-footer {
  display: block;
}
.ball {
  width: 15px;
  height: 15px;
  background-color: red;
  border-radius: 50%;
  position: absolute;
  z-index: 99;
  top: 420px;
  left: 142px;
}
.numbox{
  display: flex;
  align-items: center;
}

</style>